//
//  RequestEngine.m
//  iStore
//
//  Created by MacDev on 15/4/21.
//  Copyright (c) 2015年 mirrtalk. All rights reserved.
//

#import "RequestEngine.h"
#import "AFHTTPRequestOperationManager.h"
#import "HeaderModel.h"
#import "UserModel.h"
#import "VoiceConverter.h"


@implementation RequestEngine
////+4用户注册
+(void)userRegWithPhoneDic:(NSDictionary *)dic completed:(void(^)(NSString * errorcode,NSString *result))complete
{
    AFHTTPRequestOperationManager *manager = [self getManager];
    
    [manager POST:LOGINURL(@"user/userReg") parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
            if ([errorcode isEqualToString:@"0"])
            {
                NSDictionary * result = [responseObject objectForKey:@"RESULT"];
                if (complete)
                {
                    complete(errorcode,[NSString stringWithFormat:@"%@",result[@"userID"]]);
                    
                }
            }
            else
            {
                NSString * result = [responseObject objectForKey:@"RESULT"];
                if (complete)
                {
                    complete(errorcode,result);
                }
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
    
}
///+1登录  //登入之后保存在本地
+(void)userLoginWithDic:(NSDictionary *)dic completed:(void(^)(NSString * errorcode,NSDictionary *resultDict))complete
{
    
    AFHTTPRequestOperationManager *manager = [self getManager];
    [manager POST:LOGINURL(@"user/login") parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
        NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
        if ([errorcode isEqualToString:@"0"])
        {
            if (complete)
            {
                complete(errorcode,resultDict);
                
            }
        }
        else
        {
            if (complete)
            {
                complete(errorcode,resultDict);
            }
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
    
}




// 查询投放的广告
// 投放中
+ (void)releaseADWithDic:(NSDictionary *)dict completed:(void (^)(NSString *, NSDictionary *))complete
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager POST:LOGINURL(@"/queryReleaseAD") parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
        NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
        if ([errorcode isEqualToString:@"0"])
        {
            if (complete)
            {
                complete(errorcode,resultDict);
                
            }
        }
        else
        {
            if (complete)
            {
                complete(errorcode,resultDict);
            }
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
}
// 已完成
+ (void)finishADWithDic:(NSDictionary *)dict completed:(void (^)(NSString *, NSDictionary *))complete
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager POST:LOGINURL(@"/queryFinishedAD") parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
        NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
        if ([errorcode isEqualToString:@"0"])
        {
            if (complete)
            {
                complete(errorcode,resultDict);
                
            }
        }
        else
        {
            if (complete)
            {
                complete(errorcode,resultDict);
            }
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
}


// 已回收
+ (void)takeBackADWithDic:(NSDictionary *)dict completed:(void (^)(NSString *, NSDictionary *))complete
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager POST:LOGINURL(@"/queryTakeBackAD") parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
        NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
        if ([errorcode isEqualToString:@"0"])
        {
            if (complete)
            {
                complete(errorcode,resultDict);
                
            }
        }
        else
        {
            if (complete)
            {
                complete(errorcode,resultDict);
            }
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
}


// 撤销订单
+ (void)queryCancelADWithDic:(NSDictionary *)dict completed:(void (^)(NSString *, NSDictionary *))complete
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager POST:LOGINURL(@"/queryCancelAD") parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"________%@",dict);
        
        NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
        NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
        if ([errorcode isEqualToString:@"0"])
        {
            if (complete)
            {
                complete(errorcode,resultDict);
                
            }
        }
        else
        {
            if (complete)
            {
                complete(errorcode,resultDict);
            }
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
}

+ (void)cancelADWithDic:(NSDictionary *)dict completed:(void (^)(NSString *, NSDictionary *))complete
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager POST:LOGINURL(@"/cancelAD") parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"_____7777___%@",dict);
        
        NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
        NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
        if ([errorcode isEqualToString:@"0"])
        {
            if (complete)
            {
                complete(errorcode,resultDict);
                
            }
        }
        else
        {
            if (complete)
            {
                complete(errorcode,resultDict);
            }
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
}


//验证码的处理
+ (void) verificationCodeWith:(NSDictionary *)dict
                    completed:(void(^)(NSString * errorcode,NSDictionary *resultDict))complete{
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager POST:LOGINURL(@"/message/sendMSG") parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
            NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
            if ([errorcode isEqualToString:@"0"])
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
            else
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if (complete)
        {
            complete(@"-1",nil);
        }
        
    }];
}


//支付宝充值
+(void)userAliPayWithDic:(NSDictionary *)dic completed:(void (^)(NSString *, NSDictionary *))complete
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager POST:LOGINURL(@"user/alipay") parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
            NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
            if ([errorcode isEqualToString:@"0"])
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
            else
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
}
+(void)userAppNotifyWithDic:(NSDictionary *)dic completed:(void (^)(NSString *, NSDictionary *))complete
{
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager POST:LOGINURL(@"/user/appNotify") parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
            NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
            if ([errorcode isEqualToString:@"0"])
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
            else
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
}


//找回密码
+ (void) reSetPassWordWithDic:(NSDictionary *)dict completed:(void(^)(NSString * errorcode,NSDictionary *resultDict))complete{
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager POST:LOGINURL(@"/user/findPassword") parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
            NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
            if ([errorcode isEqualToString:@"0"])
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
            else
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
        }
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if (complete)
        {
            complete(@"-1",nil);
        }
        
    }];
}

//个人资料
+ (void) userUpdateWithDic:(NSDictionary *)dict imageArray:(NSArray*)imageArray completed:(void(^)(NSString * errorcode,NSDictionary *resultDict))complete{
    
    AFHTTPRequestOperationManager * manager = [self getManager];
    //    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:LOGINURL(@"/user/updateUser") parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        UIImage *image;
        int i=0;
        for (image in imageArray) {
            [formData appendPartWithFileData:UIImageJPEGRepresentation(imageArray[i], 0.1) name:[NSString stringWithFormat:@"file_%d",i] fileName:[NSString stringWithFormat:@"filename_%d.jpg",i] mimeType:[NSString stringWithFormat:@"image_%d/jpeg.jpg",i]];
            
            i++;
        }
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
            NSLog(@"errorcode =%@",errorcode);
            NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
            if ([errorcode isEqualToString:@"0"])
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
            else
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
    
}


//网络请求头
+(AFHTTPRequestOperationManager *)getManager
{
    NSLog(@"[HeaderModel sharedHeaderModel].sys   %@",[HeaderModel sharedHeaderModel].sys);
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    [manager.requestSerializer setValue:KSys forHTTPHeaderField:[HeaderModel sharedHeaderModel].sys];
    
    [manager.requestSerializer setValue:KSysversion forHTTPHeaderField:[HeaderModel sharedHeaderModel].sysversion];
    [manager.requestSerializer setValue:KApp forHTTPHeaderField:[HeaderModel sharedHeaderModel].app];
    [manager.requestSerializer setValue:Kappversion forHTTPHeaderField:[HeaderModel sharedHeaderModel].appversion];
    [manager.requestSerializer setValue:KPrimarykey forHTTPHeaderField:[HeaderModel sharedHeaderModel].primarykey];
    [manager.requestSerializer setValue:KNettype forHTTPHeaderField:[HeaderModel sharedHeaderModel].nettype];
    [manager.requestSerializer setValue:KDevicetype forHTTPHeaderField:[HeaderModel sharedHeaderModel].devicetype];
    NSString * accountId = [UserModel sharedUserModel].accountID;
    if ([accountId isEqualToString:@""]||accountId == nil)
    {
        accountId = @"";
    }
    [manager.requestSerializer setValue:KAccountID forHTTPHeaderField:accountId];
    return manager;
}
// 修改密码
+ (void) changePassWordWithDic:(NSDictionary *)dict completed:(void(^)(NSString * errorcode,NSDictionary *resultDict))complete{
    
    AFHTTPRequestOperationManager * manager = [self getManager];
    [manager POST:LOGINURL(@"/user/updatePassword") parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
            NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
            if ([errorcode isEqualToString:@"0"])
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
            else
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
}

//店铺信息注册
+ (void) shopShopRegWithDic:(NSDictionary *)dict imageArray:(UIImage*)image completed:(void(^)(NSString * errorcode,NSDictionary *resultDict))complete
{
    AFHTTPRequestOperationManager * manager = [self getManager];
    [manager POST:LOGINURL(@"shop/shopReg") parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.1) name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg"];
        
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
            NSLog(@"errorcode =%@",errorcode);
            NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
            if ([errorcode isEqualToString:@"0"])
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
            else
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
}
//网络数据请求
+ (void) RequestEngineWithDic:(NSDictionary *)dict URL:(NSString *)url completed:(void(^)(NSString * errorcode,NSDictionary *resultDict))complete
{
    {
        AFHTTPRequestOperationManager * manager = [self getManager];
        [manager POST:LOGINURL(url) parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
            NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
            if ([errorcode isEqualToString:@"0"])
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
            else
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
        }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            if (complete)
            {
                complete(@"-1",nil);
            }
        }];
    }
}
//支付并保存广告
+(void)payAndInsertADWithDic:(NSDictionary *)dict completed:(void(^)(NSString * errorcode,NSDictionary *resultDict))complete
{
    AFHTTPRequestOperationManager * manager = [self getManager];
    [manager POST:LOGINURL(@"payAndInsertAD") parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if ([dict[@"type"] isEqualToString:@"1"]) {
            NSFileManager *fileManager = [NSFileManager defaultManager];
            if ([fileManager fileExistsAtPath:kAudioFilePath]) {
                
                [VoiceConverter wavToAmr:kAudioFilePath amrSavePath:kAudioAmrFilePath];
                NSData *data = [NSData dataWithContentsOfFile:kAudioAmrFilePath];
                [formData appendPartWithFileData:data name:@"file" fileName:@"test.amr" mimeType:@"audio/amr"];
            }
        }
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]])
        {
            NSString * errorcode = [responseObject objectForKey:@"ERRORCODE"];
            NSLog(@"errorcode =%@",errorcode);
            NSDictionary *resultDict =[responseObject objectForKey:@"RESULT"];
            if ([errorcode isEqualToString:@"0"])
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
            else
            {
                if (complete)
                {
                    complete(errorcode,resultDict);
                }
            }
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (complete)
        {
            complete(@"-1",nil);
        }
    }];
}
@end
